package com.sf.business.process.controller;

import com.sf.business.process.domain.BusBpmnInfo;
import com.sf.business.process.service.IBusBpmnInfoService;
import com.sf.business.process.vo.BusBpmnInfoVO;
import com.sf.common.annotation.Log;
import com.sf.common.core.controller.BaseController;
import com.sf.common.core.domain.AjaxResult;
import com.sf.common.core.page.TableDataInfo;
import com.sf.common.enums.BusinessType;
import com.sf.common.utils.poi.ExcelUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 流程定义明细Controller
 * 
 * @author lh
 * @date 2025-03-26
 */
@RestController
@RequestMapping("/business/process/bpmnInfo")
public class BusBpmnInfoController extends BaseController
{
    @Autowired
    private IBusBpmnInfoService busBpmnInfoService;

    /**
     * 查询流程定义明细列表
     */
    @PreAuthorize("@ss.hasPermi('business/process:bpmnInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusBpmnInfo busBpmnInfo)
    {
        startPage();
        List<BusBpmnInfo> list = busBpmnInfoService.selectBusBpmnInfoList(busBpmnInfo);
        return getDataTable(list);
    }

    /**
     * 导出流程定义明细列表
     */
    @PreAuthorize("@ss.hasPermi('business/process:bpmnInfo:export')")
    @Log(title = "流程定义明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusBpmnInfo busBpmnInfo)
    {
        List<BusBpmnInfo> list = busBpmnInfoService.selectBusBpmnInfoList(busBpmnInfo);
        ExcelUtil<BusBpmnInfo> util = new ExcelUtil<BusBpmnInfo>(BusBpmnInfo.class);
        util.exportExcel(response, list, "流程定义明细数据");
    }

    /**
     * 获取流程定义明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('business/process:bpmnInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(busBpmnInfoService.selectBusBpmnInfoById(id));
    }

    /**
     * 新增流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('business/process:bpmnInfo:add')")
    @Log(title = "流程定义明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusBpmnInfo busBpmnInfo)
    {
        return toAjax(busBpmnInfoService.insertBusBpmnInfo(busBpmnInfo));
    }

    /**
     * 修改流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('business/process:bpmnInfo:edit')")
    @Log(title = "流程定义明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusBpmnInfo busBpmnInfo)
    {
        return toAjax(busBpmnInfoService.updateBusBpmnInfo(busBpmnInfo));
    }

    /**
     * 删除流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('business/process:bpmnInfo:remove')")
    @Log(title = "流程定义明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(busBpmnInfoService.deleteBusBpmnInfoById(id));
    }

    /**
     * 接受 multipart/form-data 类型参数时, 不能使用@RequestBody
     * @param file
     * @param vo
     * @return
     */
    @PostMapping("/flow")
    public AjaxResult flow(@RequestPart("file") MultipartFile file, BusBpmnInfoVO vo)
    {
        return toAjax(busBpmnInfoService.flow(file, vo));
    }

    /**
     * 通过 response 返回 流程文件 / 流程图
     * @param type
     * @param id
     * @param response
     * @throws IOException
     */
    @GetMapping("/{type}/{id}")
    public void getBpmn(@PathVariable("type") String type, @PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        InputStream inputStream = busBpmnInfoService.getResource(type, id);
        IOUtils.copy(inputStream, response.getOutputStream());
    }
}
